package rmmk.algorithms.preprocessing;

import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ArrayBlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import rmmk.algorithms.features.global.GlobalWordCounts;
import rmmk.algorithms.preprocessing.abstraction.IWordFilter;
import rmmk.algorithms.preprocessing.preprocessors.RemoveMostCommonWordsFromText;
import rmmk.framework.datasources.Document;

/**
 * Klasa rozbija tekst w stringu w dokumencie na listę pojedyńczych słów, wykonując
 * w międzyczasie preprocessing (usuwanie liczb, interpunkcji itp)
 * @author marcin
 *
 */
public class WordExtractor implements Runnable {
	static Logger logger = LoggerFactory.getLogger(WordExtractor.class);
	private static ArrayBlockingQueue<Document> documentQueue;
	private static List<IWordFilter> sfilters;

	public static void extractWords(List<Document> documents,
			List<IWordFilter> filters) {
		logger.info("Starting word extracting");
		documentQueue = new ArrayBlockingQueue<>(documents.size(), true,
				documents);
		sfilters = Collections.synchronizedList(filters);

		int cores = Runtime.getRuntime().availableProcessors();

		Vector<Thread> threads = new Vector<>();
		for (int i = 0; i < cores; ++i) {
			Thread thread = new Thread(new WordExtractor());
			thread.start();
			threads.add(thread);
		}

		for (Thread t : threads)
			try {
				t.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		logger.info("Removing common words");
		GlobalWordCounts.process(documents);
		RemoveMostCommonWordsFromText rmcwft = new RemoveMostCommonWordsFromText();
		rmcwft.process(documents);
		logger.info("Removing common words ended");
		
		logger.info("Word extraction completed");
	}

	@Override
	public void run() {
		//int x = 0;
		while (!documentQueue.isEmpty()) {
			Document d = documentQueue.poll();

			/*if (d.getTEXT() == null) {
				++x;
				//logger.info(d.getDATE());
			}*/

			if (d != null && d.getTEXT() != null) {
				String[] tmp = d.getTEXT().toString().split("\\s+");

				for (String word : tmp) {
					String w = word;
					for (IWordFilter filter : sfilters) {
						w = filter.processWord(w);
					}
					if (w != null)
						d.getExtractedWords().add(w);
				}
			}
		}

		//logger.info("Ilość błędnych tekstów: " + x);
	}

	class FilterRunner implements Runnable
	{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			
		}
		
	}
}
